﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using TamTreSoSinh.DBContext;

namespace TamTreSoSinh.UC
{
    public partial class ShoppingItemPart : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadShoppingCategory();
        }

        private void LoadShoppingCategory()
        {
            DBTamTreEntities dbContext = new DBTamTreEntities();
            List<ShoppingCategory> listShoppingCategory = dbContext.ShoppingCategories.Where(obj => obj.IsVisible).OrderBy(obj => obj.Priority).ToList();

            rptShoppingCategory.DataSource = listShoppingCategory;
            rptShoppingCategory.DataBind();
        }

        protected void rptShoppingCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ShoppingCategory shoppingCategory = (ShoppingCategory)e.Item.DataItem;
            Repeater rptShoppingItem = (Repeater)e.Item.FindControl("rptShoppingItem");
            Label lbl;
            lbl = (Label)e.Item.FindControl("lblCategoryTitle");
            lbl.Text = shoppingCategory.Title;

            List<Product> listProduct = shoppingCategory.Products.Where(obj => obj.IsOnCategory).OrderBy(obj => obj.Priority).ToList();

            rptShoppingItem.DataSource = listProduct;
            rptShoppingItem.DataBind();
        }

        protected void rptShoppingItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HyperLink hpl;
            Label lbl;
            Product product = (Product)e.Item.DataItem;
            hpl = (HyperLink)e.Item.FindControl("hplImgService");
            hpl.ImageUrl = product.ImagePath;
            hpl.NavigateUrl = "/ProductDetailPage.aspx?Id=" + product.Id;

            hpl = (HyperLink)e.Item.FindControl("hplTitleService");
            hpl.Text = product.ProductName;
            hpl.NavigateUrl = "/ProductDetailPage.aspx?Id=" + product.Id;

            if (product.OldPrice.HasValue)
            {
                lbl = (Label)e.Item.FindControl("lblOldPrice");
                //lbl.Visible = true;
                lbl.Text = string.Format("{0:#,##0}", product.OldPrice.Value).Replace(",", ".") + " VND";
                HtmlGenericControl spanOldPrice = (HtmlGenericControl)e.Item.FindControl("spanOldPrice");
                spanOldPrice.Visible = true;
            }

            lbl = (Label)e.Item.FindControl("lblCurrentPrice");
            lbl.Text = string.Format("{0:#,##0}", product.CurrentPrice).Replace(",", ".") + " VND";
        }
    }
}